<?php defined('SYSPATH') or die('No direct script access.');
                          
class Education_Controller extends Template_Controller 
{
	public $template = 'layout/education';
	
	public function __construct()
	{
		parent::__construct();

		$this->db = new Database();

		$this->template->menu = '';
		$this->template->content = '';
		$this->template->category_id = '';
		
		$rs = array();
		$i = 0;
		$result = $this->db->query('SELECT * FROM article_categories WHERE left_id>1 AND right_id <(SELECT right_id FROM article_categories WHERE id=2) AND layer_id<=2 ORDER BY left_id');
		foreach($result as $item)
		{
			$rs[$i]['category'] = $item;
			$rs[$i]['article'] = $this->db->query('SELECT a.* FROM articles AS a INNER JOIN articles_categories AS ac ON a.id=ac.article_id WHERE ac.category_id='.$item->id.' ORDER BY ac.sort_order');
			$i++;
		}
				
		$this->template->leftmenu = $rs;
	}
	
	public function index()
	{
		$model = new Article_Model();
		$view = new View('education/home');
		$view->overview = $model->where('name', 'education-home-overview')->find();
		$model = new Article_Model();
		$view->whatson = $model->where('name', 'education-home-whatson')->find();
		$model = new Article_Model();
		$view->news = $model->where('name', 'education-home-news')->find();
		$this->template->content =  $view->render();
	}
		
	public function category($category_id)
	{
		$view = new View('education/category');
		$view->category = new Article_Category_Model($category_id);
		$this->template->category_id = $category_id;
		$this->template->content =  $view->render();
	}
	
	public function article($category_id, $article_id)
	{
		$view = new View('education/article');
		$view->category = new Article_Category_Model($category_id);
		$view->article = new Article_Model($article_id);
		
		$this->template->category_id = $category_id;
		$this->template->content =  $view->render();
	}
}